﻿
Partial Class WebForm_ManageEcolife_SearchEcolifeData
    Inherits System.Web.UI.UserControl
    Private NoMoreItem As String = "--沒有更多標的物--"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindLogType()
            Me.BindTownID()
            Me.BindVillageID()
            Me.BindBlogGroup()
            Me.BindBlogType()
            Me.BindItemGroup1()
            Me.BindItemGroup2()
            Me.BindItemGroup3()
            Me.BindItemGroup4()
            Me.BindActive()
        End If
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(Me.SearchCleanPlanControl1)
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(Me.SearchDirtyHeaderControl1)
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

#Region "GV事件"
    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            '不同日誌連結網址不同
            Dim hlnikCaseNo As HyperLink = TryCast(e.Row.FindControl("hlinkCaseNo"), HyperLink)
            Dim hlinkReportDescription As HyperLink = TryCast(e.Row.FindControl("hlinkReportDescription"), HyperLink)
            Dim LinkUrl As String = "http://ecolife.epa.gov.tw"
            Select Case Left(hlnikCaseNo.Text, 1)
                Case "C" '清理日誌
                    LinkUrl &= "/clean/" & hlnikCaseNo.Text
                Case "P" '巡檢日誌
                    LinkUrl &= "/inspect/" & hlnikCaseNo.Text
                Case "R" '通報日誌
                    LinkUrl &= "/announce/" & hlnikCaseNo.Text
            End Select
            hlnikCaseNo.NavigateUrl = LinkUrl
            hlinkReportDescription.NavigateUrl = LinkUrl

            '已匯入的資料則不顯示匯入按鈕
            Dim btnImport As Button = TryCast(e.Row.FindControl("btnImport"), Button)
            If DataBinder.Eval(e.Row.DataItem, "Active").ToString() = "Y" Then
                btnImport.Visible = True

                '不同日誌按下匯入後的功能不同
                Select Case Left(hlnikCaseNo.Text, 1)
                    Case "C", "P" '清理日誌,巡檢日誌
                        If DataBinder.Eval(e.Row.DataItem, "ReportCaseNo").ToString() = "" Then
                            '(1)無通報日誌號碼,則可選擇服務計畫
                            btnImport.CommandName = "AddCleanPlan"
                        Else
                            '(2)有通報日誌號碼,則前往髒亂點通報
                            btnImport.CommandName = "AddDirtyHeader"
                        End If
                    Case "R" '通報日誌
                        '前往新增髒亂點通報的頁面
                        btnImport.CommandName = "AddWorkHeader"
                End Select
            Else
                btnImport.Visible = False
            End If

            'ScriptManager.GetCurrent(Page).RegisterPostBackControl(TryCast(e.Row.FindControl("btnImport"), Button))
        End If
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

        If e.CommandName = "AddCleanPlan" Or e.CommandName = "AddCleanPlan" Or e.CommandName = "AddCleanPlan" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Me.CaseNo = Me.gv.DataKeys(row.RowIndex).Item("CaseNo").ToString()
            Me.ReportCaseNo = Me.gv.DataKeys(row.RowIndex).Item("ReportCaseNo").ToString()
        End If

        'MyObj.WriteLog(e.CommandName)
        If e.CommandName = "AddCleanPlan" Then
            '開窗取值
            Me.SearchCleanPlanControl1.Show()
        ElseIf e.CommandName = "AddDirtyHeader" Then
            Dim dat As New clsData
            Dim sql As New MyStringBuilder
            Dim dt As Data.DataTable

            sql.AppendFormat(" select e.*,f.FindDirtyHeaderID ")
            sql.AppendFormat(" from EcolifeDataView e ")
            sql.AppendFormat(" inner join FindDirtyHeader f on f.EcolifeCaseNo = e.CaseNo ")
            sql.AppendFormat(" where e.Active='N' ")

            dt = dat.GetData(sql)
            If dt.Rows.Count > 0 Then
                Me.FindDirtyHeaderID = dt.Rows(0)("FindDirtyHeaderID").ToString()
                SearchDirtyHeaderControl1.InitDataAndShow()
            Else
                MyObj.ShowMessage(Me.GetLocalResourceObject("NeedReport"))
            End If
        ElseIf e.CommandName = "AddWorkHeader" Then
            RaiseEvent AddWorkHeader(Me, New EventArgs)
        End If
    End Sub
#End Region

    '服務計畫開窗取值回來-PlanName
    Protected Sub SearchCleanPlanControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchCleanPlanControl1.SelectedIndexChanged
        Me.SelectedPlanID = SearchCleanPlanControl1.SelectedPlanID
        RaiseEvent AddCleanPlan(Me, New EventArgs)
    End Sub

    '髒亂點開窗取值回來-PlanName
    Protected Sub SearchDirtyHeaderControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchDirtyHeaderControl1.SelectedIndexChanged
        Me.SelectedPlanID = Me.SearchDirtyHeaderControl1.SelectedValue
        RaiseEvent AddDirtyHeader(Me, New EventArgs)
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat(" select distinct e.CaseNo,e.ReportCaseNo,e.ReportDescription,e.ImportDate,e.Active ")
        Select Case Me.ddlLogType.SelectedValue
            Case "通報日誌"
                sql.AppendFormat(",ReportDate as TheDate ")
            Case Else
                sql.AppendFormat(",CleanDateStart as TheDate ")
        End Select
        sql.AppendFormat(",u.UserName ")
        sql.AppendFormat(",case u.UserType when 'V' then a.OrganizationName else b.OrganizationName end as 'OrganizationName' ")
        sql.AppendFormat(" from EcolifeDataView e ")
        sql.AppendFormat(" left join [User] u on u.UserID = e.ImportBy ")
        sql.AppendFormat(" left join ")
        sql.AppendFormat(" (")
        sql.AppendFormat(" select v.VolunteerID as UserID, vo.VOrganizationName as OrganizationName ")
        sql.AppendFormat(" from VolunteerDistinctView v ")
        sql.AppendFormat(" left join VolunteerView vv on v.VolunteerID = vv.VolunteerID ")
        sql.AppendFormat(" left join VOrganization vo on vv.VOrganizationID = vo.VOrganizationID ")
        sql.AppendFormat(" ) ")
        sql.AppendFormat(" a on a.UserID = u.UserID ")
        sql.AppendFormat(" left join ")
        sql.AppendFormat(" (")
        sql.AppendFormat(" select u.UserID ")
        sql.AppendFormat(" ,o.OrganizationName ")
        sql.AppendFormat(" from [InternalUser] u ")
        sql.AppendFormat(" inner join OrganizationView o on u.UOrganizationID = o.UOrganizationID ")
        sql.AppendFormat(" ) ")
        sql.AppendFormat(" b on b.UserID = u.UserID ")

        sql.AppendFormat(" where 1=1 ")
        sql.AppendFormat(" and e.EcolifeID='{0}' ", Me.txtEcolifeID.Text.Replace("'", "''"))

        sql.AppendFormat(GetWhereSQL())
        sql.AppendFormat(Me.gv.OrderBySQL)
        'MyObj.WriteLog(sql.ToString())
        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        '日誌類型
        If Me.ddlLogType.SelectedIndex <> -1 Then
            If Me.ddlLogType.SelectedValue <> "" Then
                sql.AppendFormat(" and e.LogType='{0}' ", Me.ddlLogType.SelectedValue)
            End If
        End If

        '查詢日期
        Select Case Me.ddlLogType.SelectedValue
            Case "通報日誌"
                sql.AppendFormat(" and ReportDate>='{0:yyyy/MM/dd}'", Me.txtCleanDateStart.Value)
                sql.AppendFormat(" and ReportDate<=DateAdd(dd,1,'{0:yyyy/MM/dd}')", Me.txtCleanDateEnd.Value)
            Case Else
                sql.AppendFormat(" and CleanDateStart>='{0:yyyy/MM/dd}'", Me.txtCleanDateStart.Value)
                sql.AppendFormat(" and CleanDateStart<=DateAdd(dd,1,'{0:yyyy/MM/dd}')", Me.txtCleanDateEnd.Value)
        End Select

        '行政區域
        If Me.ddlTownID.SelectedIndex <> -1 Then
            If Me.ddlTownID.SelectedValue <> "" Then
                sql.AppendFormat(" and e.LocationB like '%{0}%' ", Me.ddlTownID.SelectedItem.Text)
                If Me.ddlVillageID.SelectedIndex <> -1 Then
                    If Me.ddlVillageID.SelectedValue <> "" Then
                        sql.AppendFormat(" and e.LocationC like '%{0}%' ", Me.ddlVillageID.SelectedItem.Text)
                    End If
                End If
            End If
        End If

        '部落格類型
        If Me.ddlBlogGroup.SelectedIndex <> -1 Then
            If Me.ddlBlogGroup.SelectedValue <> "" Then
                sql.AppendFormat(" and e.BlogType in ( ")
                sql.AppendFormat(" select Text from CodeMapping where TableName='Ecolife_BlogGroup_{0}' ", Me.ddlBlogGroup.SelectedValue)
                sql.AppendFormat(" and ColumnName='BlogType' and Active='Y' ")
                If Me.ddlBlogType.SelectedIndex <> -1 Then
                    If Me.ddlBlogType.SelectedValue <> "" Then
                        sql.AppendFormat(" and Text='{0}' ", Me.ddlBlogType.SelectedItem.Text)
                    End If
                End If
                sql.AppendFormat(" ) ")
            End If
        End If

        '通報物件
        Dim TableName As String = "Ecolife_ItemGroup_" & Me.ddlItemGroup1.SelectedValue
        If Me.ddlItemGroup4.SelectedValue <> "" Then
            sql.AppendFormat(" and e.ItemName ='{0}' ", Me.ddlItemGroup4.SelectedItem.Text)
        ElseIf Me.ddlItemGroup3.SelectedValue <> "" Then
            TableName &= "_" & Me.ddlItemGroup2.SelectedValue
            TableName &= "_" & Me.ddlItemGroup3.SelectedValue
            TableName &= "_"
            sql.AppendFormat(" and e.ItemName in ")
            sql.AppendFormat(" ( ")
            sql.AppendFormat("  select '{0}' ", Me.ddlItemGroup3.SelectedItem.Text)
            sql.AppendFormat("  union ")
            sql.AppendFormat("  select Text from CodeMapping where 1=1 ")
            sql.AppendFormat("  and TableName+'_' like '%{0}%' ", TableName)
            sql.AppendFormat("  and ColumnName='{0}' ", "Itemname")
            sql.AppendFormat(" ) ")
        ElseIf Me.ddlItemGroup2.SelectedValue <> "" Then
            TableName &= "_" & Me.ddlItemGroup2.SelectedValue
            TableName &= "_"
            sql.AppendFormat(" and e.ItemName in ")
            sql.AppendFormat(" ( ")
            sql.AppendFormat("  select '{0}' ", Me.ddlItemGroup2.SelectedItem.Text)
            sql.AppendFormat("  union ")
            sql.AppendFormat("  select Text from CodeMapping where 1=1 ")
            sql.AppendFormat("  and TableName+'_' like '%{0}%' ", TableName)
            sql.AppendFormat("  and ColumnName='{0}' ", "Itemname")
            sql.AppendFormat(" ) ")
        ElseIf Me.ddlItemGroup1.SelectedValue <> "" Then
            TableName &= "_"
            sql.AppendFormat(" and e.ItemName in ")
            sql.AppendFormat(" ( ")
            sql.AppendFormat("  select '{0}' ", Me.ddlItemGroup1.SelectedItem.Text)
            sql.AppendFormat("  union ")
            sql.AppendFormat("  select Text from CodeMapping where 1=1 ")
            sql.AppendFormat("  and TableName+'_' like '%{0}%' ", TableName)
            sql.AppendFormat("  and ColumnName='{0}' ", "Itemname")
            sql.AppendFormat(" ) ")
        End If

        '匯入狀況
        If Me.ddlActive.SelectedIndex <> -1 Then
            If Me.ddlActive.SelectedValue <> "" Then
                sql.AppendFormat(" and e.Active='{0}' ", Me.ddlActive.SelectedValue)
            End If
        End If

        Return sql.ToString()
    End Function

    Protected Sub ddlTownID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindVillageID()
    End Sub

    Protected Sub ddlBlogGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindBlogType()
    End Sub

    Protected Sub ddlItemGroup1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlItemGroup1.SelectedIndexChanged
        Me.BindItemGroup2()
        Me.BindItemGroup3()
        Me.BindItemGroup4()
    End Sub

    Protected Sub ddlItemGroup2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlItemGroup2.SelectedIndexChanged
        Me.BindItemGroup3()
        Me.BindItemGroup4()
    End Sub

    Protected Sub ddlItemGroup3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlItemGroup3.SelectedIndexChanged
        Me.BindItemGroup4()
    End Sub

#Region "繫結資料"
    Private Sub BindLogType()
        Dim ddl As DropDownList = Me.ddlLogType
        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataSource = MyObj.GetCodeMappingData("Ecolife", "LogType")
        ddl.DataBind()

        'ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem)
    End Sub

    Private Sub BindTownID()
        Dim ddl As DropDownList = Me.ddlTownID

        ddl.DataTextField = "RegionName"
        ddl.DataValueField = "RegionID"
        ddl.DataSource = MyObj.GetTownData("TPC")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Private Sub BindVillageID()
        Dim ddl As DropDownList = Me.ddlVillageID

        ddl.DataTextField = "RegionName"
        ddl.DataValueField = "RegionID"
        ddl.DataSource = MyObj.GetVillageData(Me.ddlTownID.SelectedValue)
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Private Sub BindBlogGroup()
        Dim ddl As DropDownList = Me.ddlBlogGroup
        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataSource = MyObj.GetCodeMappingData("Ecolife", "BlogGroup")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem)
    End Sub

    Private Sub BindBlogType()
        Dim ddl As DropDownList = Me.ddlBlogType

        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataSource = MyObj.GetCodeMappingData("Ecolife_BlogGroup_" & Me.ddlBlogGroup.SelectedValue, "BlogType")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Private Sub BindItemGroup1()
        Dim ddl As DropDownList = Me.ddlItemGroup1
        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataSource = MyObj.GetCodeMappingData("Ecolife", "ItemGroup")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem)
    End Sub

    Private Sub BindItemGroup2()
        Dim ddl As DropDownList = Me.ddlItemGroup2

        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        Dim TableName As String = "Ecolife_ItemGroup_" & Me.ddlItemGroup1.SelectedValue
        ddl.DataSource = MyObj.GetCodeMappingData(TableName, "ItemName")
        ddl.DataBind()

        If ddl.Items.Count = 0 And (Me.ddlItemGroup1.SelectedValue <> "" OrElse Me.ddlItemGroup1.SelectedItem.Text = NoMoreItem) Then
            ddl.Items.Insert(0, New ListItem(NoMoreItem, ""))
            ddl.Enabled = False
        Else
            ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
            ddl.Enabled = True
        End If
    End Sub

    Private Sub BindItemGroup3()
        Dim ddl As DropDownList = Me.ddlItemGroup3

        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        Dim TableName As String = "Ecolife_ItemGroup_" & Me.ddlItemGroup1.SelectedValue
        TableName &= "_" & Me.ddlItemGroup2.SelectedValue
        ddl.DataSource = MyObj.GetCodeMappingData(TableName, "ItemName")
        ddl.DataBind()

        If ddl.Items.Count = 0 And (Me.ddlItemGroup2.SelectedValue <> "" OrElse Me.ddlItemGroup2.SelectedItem.Text = NoMoreItem) Then
            ddl.Items.Insert(0, New ListItem(NoMoreItem, ""))
            ddl.Enabled = False
        Else
            ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
            ddl.Enabled = True
        End If
    End Sub

    Private Sub BindItemGroup4()
        Dim ddl As DropDownList = Me.ddlItemGroup4

        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        Dim TableName As String = "Ecolife_ItemGroup_" & Me.ddlItemGroup1.SelectedValue
        TableName &= "_" & Me.ddlItemGroup2.SelectedValue
        TableName &= "_" & Me.ddlItemGroup3.SelectedValue
        ddl.DataSource = MyObj.GetCodeMappingData(TableName, "ItemName")
        ddl.DataBind()

        If ddl.Items.Count = 0 And (Me.ddlItemGroup3.SelectedValue <> "" OrElse Me.ddlItemGroup3.SelectedItem.Text = NoMoreItem) Then
            ddl.Items.Insert(0, New ListItem(NoMoreItem, ""))
            ddl.Enabled = False
        Else
            ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
            ddl.Enabled = True
        End If
    End Sub

    '匯入狀況
    Private Sub BindActive()
        Dim ddl As DropDownList = Me.ddlActive

        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataSource = MyObj.GetCodeMappingData("Ecolife", "Active")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLSelectAllItem)

        MyObj.MatchItem(ddl, "Y")
    End Sub
#End Region

#Region "屬性"
    Public Property EcolifeID() As String
        Get
            Return ViewState("EcolifeID")
        End Get
        Set(ByVal value As String)
            ViewState("EcolifeID") = value
            Me.txtEcolifeID.Text = ViewState("EcolifeID")
        End Set
    End Property

    Public Property CaseNo() As String
        Get
            Return ViewState("CaseNo")
        End Get
        Set(ByVal value As String)
            ViewState("CaseNo") = value
        End Set
    End Property

    Public Property ReportCaseNo() As String
        Get
            Return ViewState("ReportCaseNo")
        End Get
        Set(ByVal value As String)
            ViewState("ReportCaseNo") = value
        End Set
    End Property

    Public Property SelectedPlanID() As String
        Get
            Return ViewState("SelectedPlanID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedPlanID") = value
        End Set
    End Property

    Public Property FindDirtyHeaderID() As String
        Get
            Return ViewState("FindDirtyHeaderID")
        End Get
        Set(ByVal value As String)
            ViewState("FindDirtyHeaderID") = value
        End Set
    End Property
#End Region

#Region "Raise事件"
    Public Event AddCleanPlan(ByVal source As Object, ByVal e As EventArgs)
    Public Event AddWorkHeader(ByVal source As Object, ByVal e As EventArgs)
    Public Event AddDirtyHeader(ByVal source As Object, ByVal e As EventArgs)
#End Region

End Class
